<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
    <meta name="keywords" content="" />
    <meta name="description" content="">
    <link href="css/bulma.min.css" rel="stylesheet">
    <link href="css/github-markdown.min.css" rel="stylesheet">
    <link rel="stylesheet" href="css/app.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/config.js"></script>
    <script src="js/urlutil.js"></script>
    <script src="js/marked.min.js"></script>
    <script src="js/loginutil.js"></script>

</head>



<body>
    <nav id="navbar" class="navbar has-shadow">
        <div class="container">
            <div class="navbar-brand">
                <div class="navbar-burger burger" data-target="navMenu">
                    <span></span>
                    <span></span>
                    <span></span>
                </div>
            </div>
            <div id="navMenu" class="navbar-menu">
                <div class="navbar-start">
                    <a class="navbar-item" href="index.html">主页</a>
                    <a class="navbar-item" href="about.html">关于</a>
                </div>
                <div class="navbar-end">
                    <div id="login_div" style="display: none;" class="navbar-item">
                        <a id="category_mangager" style="display: none;background-color: #315790;" class="button is-small is-primary"
                           href="category.html">分类管理</a>&nbsp;&nbsp;&nbsp;
                        <a class="button is-small is-primary" style="background-color: #315790;" href="add-article.html">写文章</a>&nbsp;&nbsp;&nbsp;&nbsp;
                        <a id="login_username" href="user-articlelist.html" style="color: #373737"></a>&nbsp;&nbsp;&nbsp;&nbsp;
                        <a href="javascript:logout();" style="color: #373737">退出</a>
                    </div>

                    <div id="unlogin_div" class="navbar-item">
                        <a class="button is-small is-primary" style="background-color: #315790;" href="register.html">注册</a>&nbsp;&nbsp;&nbsp;&nbsp;
                        <a class="button is-small" href="login.html">登录</a>
                    </div>
                </div>
            </div>
        </div>
    </nav>


    <script>
        jQuery(function () {
            var login_username = window.localStorage.getItem("username");
            if (login_username == null) {
                // 未登录
                jQuery("#unlogin_div").show();
            } else {
                // 已经登录
                jQuery("#unlogin_div").hide();
                jQuery("#login_username").html(login_username);
                jQuery("#login_username").attr("href", "user-articlelist.html?uid=" + window.localStorage.getItem("uid"));
                jQuery("#login_div").show();
                // 管理员权限判定
                if (window.localStorage.getItem("manager") == 1) {
                    jQuery("#category_mangager").show();    // 让文章管理按钮显示
                }
            }
        });
    </script>
    <div class="container margin-top">
        <div class="columns">
            <div class="column is-8 is-offset-2">
                <div class="box markdown-body">
                    <h1 id="title"></h1>
                    <div>
                        作者：<a href="" id="username"></a>
                        <div class="is-pulled-right" id="article_edit" style="display: none;">
                            <a id="edit_article_href" class="button is-small is-primary" href="">编辑</a>
                            <a id="del_article_href" class="button is-small is-danger" href="">删除</a>
                        </div>
                    </div>
                    <p id="content">

                    </p>
                    <p>
                        <a href="javascript:void(0);" style="background-color: #0298c9;" class="tag is-primary" id="category"></a>
                    </p>
                </div>
                <section class="margin-top box">
                    <div class="media">
                        <div class="media-content">
                            <div class="field">
                                <p class="control">
                                    <textarea id="comment_content" class="textarea" placeholder="发表评论…"></textarea>
                                </p>
                            </div>
                            <button class="button is-info is-pulled-right" onclick="doAddComment()">评论</button>
                        </div>
                    </div>
                    <hr>
                    <div id="commentList">

                    </div>
                </section>
            </div>
        </div>
    </div>
    <script>
        // 从当前 URL 中获取 aid
        var aid = getUrlParam("aid");
        var isOwner = false;


        // 文章删除功能
        function doArticleDel(aid) {
            if (confirm("确认删除?")) {
                jQuery.ajax({
                    url: req_base_url + "article/delByAid",
                    method: "POST",
                    headers: {
                        "Authorization": window.localStorage.getItem(token_key)
                    },
                    data: {
                        "aid": aid
                    },
                    success: function (res) {
                        // 4. 将执行的结果展示到前端
                        if (res != null && res.code == 200) {
                            // 成功
                            alert("删除成功!");
                            location.href = "user-articlelist.html?uid=" + window.localStorage.getItem("uid");
                        } else {
                            // 失败
                            alert("删除失败 " + res.msg);
                        }

                    }
                })
            }
        }



        // 初始化文章详情信息
        function initArticle() {
            // 参数校验
            if (aid == null || aid <= 0) {
                location.href = "404.html";
                return false;
            }
            jQuery.ajax({
                url: req_base_url + "article/getById",
                method: "GET",
                data: {
                    "aid": aid
                },
                success: function (res) {
                    // 4. 将执行的结果展示到前端
                    if (res != null && res.code == 200 && res.data != null) {
                        // 成功
                        jQuery("#title").html(res.data.title);
                        jQuery("#username").html(res.data.username);
                        jQuery("#username").attr("href", "user-articlelist.html?uid=" + res.data.uid)
                        jQuery("#category").html(res.data.category);
                        jQuery("#content").html(marked(res.data.content));
                        jQuery("#edit_article_href").attr("href", "edit-article.html?aid=" + res.data.aid);
                        jQuery("#del_article_href").attr("href", "javascript:doArticleDel(" + res.data.aid + ");");
                        // 进行文章归属的判断
                        var uid = window.localStorage.getItem("uid");
                        if (uid != null && uid > 0) {
                            if (uid == res.data.uid) {
                                isOwner = true;
                                // 显示操作按钮 [修改/删除]
                                jQuery("#article_edit").show();
                            }
                        }
                    } else {
                        // 失败
                        location.href = "error.html";
                        alert("初始化失败 " + res.msg);
                    }

                }
            })
        }


        // 初始化评论列表
        function initCommentList() {
            jQuery.ajax({
                url: req_base_url + "comment/listByAid",
                method: "GET",
                data: {
                    "aid": aid
                },
                success: function (res) {
                    // 4. 将执行的结果展示到前端
                    if (res != null && res.code == 200 && res.data != null) {
                        // 成功
                        var commentHtml = "";
                        for (var i = 0; i < res.data.length; i++) {
                            var comment = res.data[i];
                            commentHtml += '<article class="media comment">';
                            commentHtml += '<figure class="media-left">';
                            commentHtml += '<p class="image is-24x24">';
                            commentHtml += '<img src="image/avatar04.jpg">';
                            commentHtml += '</p>';
                            commentHtml += '</figure>';
                            commentHtml += '<div class="media-content">';
                            commentHtml += '<div class="content">';
                            commentHtml += '<p>';
                            commentHtml += '<a href="user-articlelist.html?uid=' + comment.uid + '">';
                            commentHtml += '<strong>' + comment.username + '</strong>';
                            commentHtml += '</a>';
                            commentHtml += '<small style="font-size: .7835em">&nbsp;&nbsp;'+ comment.time +'</small>';
                            commentHtml += '<br>';
                            commentHtml += comment.content;
                            commentHtml += '</p>';
                            commentHtml += '</div>';
                            commentHtml += '<nav>';
                            commentHtml += '</nav>';
                            commentHtml += '</div>';
                            commentHtml += '<div class="media-right is-invisible">';
                            commentHtml += '<button id="reply" class="button is-small is-primary">回复</button>';
                            commentHtml += '&nbsp;&nbsp;<a style="display:none;" name="isowner" href="javascript:doDelComment(' + comment.cid
                                + ');" class="button is-small">删除</a>';
                            commentHtml += '</div>';
                            commentHtml += '</article>';
                        }
                        jQuery("#commentList").html(commentHtml);
                        if (isOwner) {
                            jQuery("[name='isowner']").each(function () {
                                jQuery(this).show();
                            });
                        }
                    } else {
                        // 失败
                        location.href = "error.html";
                        alert("初始化失败 " + res.msg);
                    }

                }
            })
        }

        // 初始化页面数据
        function initPage() {
            // 1. 文章详情展示
            initArticle();

            // 2. 评论列表展示
            initCommentList();
        }
        initPage();


        // 添加评论
        function doAddComment() {
            var comment_content = jQuery("#comment_content");
            // 非空效验
            if (comment_content.val().trim() == "") {
                alert("请输入评论内容!");
                comment_content.focus();
                return false;
            }
            jQuery.ajax({
                url: req_base_url + "comment/add",
                method: "POST",
                headers: {
                    "Authorization": window.localStorage.getItem(token_key)
                },
                data: {
                    "aid": aid,
                    "content": comment_content.val()
                },
                success: function (res) {
                    // 4. 将执行的结果展示到前端
                    if (res != null && res.code == 200) {
                        // 成功
                        alert("评论成功!");
                        location.href = location.href;
                    } else {
                        // 失败
                        location.href = "error.html";
                        alert("评论失败 " + res.msg);
                    }

                }
            })
        }


        // 评论删除功能
        function doDelComment(cid) {
            if (confirm("是否确认删除?")) {
                jQuery.ajax({
                    url: req_base_url + "comment/del",
                    method: "POST",
                    headers: {
                        "Authorization": window.localStorage.getItem(token_key)
                    },
                    data: {
                        "cid": cid
                    },
                    success: function (res) {
                        // 4. 将执行的结果展示到前端
                        if (res != null && res.code == 200) {
                            // 成功
                            alert("删除成功!");
                            location.href = location.href;
                        } else {
                            // 失败
                            location.href = "error.html";
                            alert("删除失败 " + res.msg);
                        }
                    }
                })
            }


        }


    </script>
</body>

</html>